package com.gz.mylife.adapter;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.gz.mylife.R;
import com.gz.mylife.app.Browser;
import com.gz.mylife.app.BrowserPay;
import com.gz.mylife.app.FragmentMallBrandedProductsActivity;
import com.gz.mylife.app.ProductDetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.zl.smartmall.library.po.AdvertInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

public class AdvertFragment extends Fragment {

	private static final String TAG = AdvertFragment.class.getSimpleName();
	private String mPictureUrl;
	private DisplayImageOptions mOptions;
	private ImageView mPicture;

	/**
	 * 创建一个广告的fragment
	 * 
	 * @param advertInfo
	 *            广告数据。如果为空将不响应任何事件
	 * @param advertUrl
	 *            广告图片URL
	 * @return
	 */
	public static AdvertFragment newInstance(AdvertInfo advertInfo,
			String advertUrl) {
		HashMap<String, AdvertInfo> map = new HashMap<String, AdvertInfo>();
		map.put("advert", advertInfo);
		AdvertFragment fragment = new AdvertFragment();
		Bundle bundle = new Bundle();
		bundle.putSerializable("advert", map);
		bundle.putString("advertUrl", advertUrl);
		fragment.setArguments(bundle);

		return fragment;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		CrashHandler.d(TAG, "AdvertFragment onCreate");

		mPictureUrl = getArguments().getString("advertUrl");
		mOptions = buildPictureOptions();
		mPicture = new ImageView(getActivity());
		mPicture.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		mPicture.setScaleType(ScaleType.FIT_XY);
		Object tag = ((HashMap<String, AdvertInfo>) getArguments()
				.getSerializable("advert")).get("advert");
		if (tag != null) {// 判断空是为了兼容不是AdvertInfo对象的封装, 如商品详情的大图
			mPicture.setTag(tag);
			mPicture.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AdvertInfo advert = (AdvertInfo) v.getTag();
					if (advert.getWeburl() != null&& !advert.getWeburl().equals("")) {
						Intent intent = new Intent();
						intent.setClass(getActivity(), BrowserPay.class);
						intent.putExtra("url", advert.getWeburl());
						startActivity(intent);
					} else {
						startActivity(retriveIntent(getActivity()
								.getApplicationContext(), advert));
					}

				}
			});
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (container == null) {
			CrashHandler.d(TAG, "container为空");
			return null;
		}

		ImageLoader.getInstance().displayImage(mPictureUrl, mPicture, mOptions);
		CrashHandler.d(TAG, "广告url = " + mPictureUrl);
		return mPicture;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		CrashHandler.d(TAG, "AdvertFragment onActivityCreated");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		CrashHandler.d(TAG, "Fragment's onDestroyView");

		if (mPicture != null && mPicture.getParent() != null) {
			((ViewGroup) mPicture.getParent()).removeView(mPicture);
		}
	}

	private DisplayImageOptions buildPictureOptions() {
		return new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
				.bitmapConfig(Bitmap.Config.ARGB_8888).cacheInMemory(true)
				.cacheOnDisk(true)
				.showImageOnLoading(R.drawable.ic_load_image_square_default)
				.showImageOnFail(R.drawable.ic_load_image_square_fail)
				.showImageForEmptyUri(R.drawable.ic_load_image_square_fail)
				.imageScaleType(ImageScaleType.EXACTLY).build();
	}

	private Intent retriveIntent(Context context, AdvertInfo advertInfo) {
		Intent intent = new Intent();

		switch (advertInfo.getAdvertType()) {

		case Constants.ADVERT_TYPE_BRANDS_LIST:
			intent.setClass(context, FragmentMallBrandedProductsActivity.class);
			intent.putExtra("brandId",
					(Integer) advertInfo.getValueMap().get("brand_id"));
			intent.putExtra("name",
					(String) advertInfo.getValueMap().get("brand_name"));
			break;

		case Constants.ADVERT_TYPE_PRODUCT_DETAIL:
			intent.setClass(context, ProductDetail.class);
			intent.putExtra("productId", (Integer) advertInfo.getValueMap()
					.get("product_id"));
			intent.putExtra("shopId",
					(Integer) advertInfo.getValueMap().get("shop_id"));
			break;
		}

		return intent;
	}
}